import { message } from 'antd'
import { request } from './request'

// 登陆
const UserLogin = (params: any) => {
  let result
  request
    .get({ url: '/user/login', data: params })
    .then((res) => {
      result = res
    })
    .catch((e) => {})
  return result
}

// 用户注册
const UserReg = (params: any) => {
  let result
  request
    .post({ url: '/reg', data: params })
    .then((res) => {
      result = res
    })
    .catch((e) => {})

  return result
}

const Delete = (url: string, val?: any) => {
  request
    .delete({
      url: url + val,
    })
    .then((data: any) => {
      if (data.code === 200) {
        message.success('删除数据成功！')
        return
      }
      message.error('删除失败！')
    })
    .catch(() => {
      message.error('请求失败！')
    })
}

const Update = (url: string, val?: string) => {
  request
    .put({
      url: url + val,
    })
    .then((data: any) => {
      if (data.code === 200) {
        message.success('更新数据成功！')
        return
      }
      message.error('更新失败！')
    })
    .catch(() => {
      message.error('请求失败！')
    })
}

const Add = (url: string, val?: string) => {
  request
    .post({
      url: url + val,
    })
    .then((data: any) => {
      if (data.code === 200) {
        message.success('添加数据成功！')
        return
      }
      message.error('添加失败！')
    })
    .catch(() => {
      message.error('添加失败！')
    })
}

export { UserLogin, UserReg, Delete, Update, Add }
